<?php

class CF_Multiselect{

	/**
	* @var array $gotopages The array of pages identified by the action, for the system to link to after ahe action
	* example:  $gotopages['save'] = someformpage.php?mode=save
	*/
	var $gotopages = array();	
	var $table_name = 'thistable';
	var $method = 'GET';
	var $fieldspec;
	var $actionpage;

	function CF_Multiselect($fieldspec, $table_name, $actionpage){
		$this->fieldspec = $fieldspec;
		$this->table_name = $table_name;
		$this->actionpage = $actionpage;
	
	
	}
	
	/**
	 *  make_multi_act_row  Method will produce a multipe-action row to change
	  * all checked rows at once.
	  * @param string $method {'get' or 'post'} for form return method
	  *
	 */
	function make_multi_act_row($method = "get"){
		$none_entry[1]['keyvalue'] = '';
		$none_entry[1]['showvalue'] = '';
	

		$msrow .= "<form name='ms_form'  id='" . $this->table_name . "_ms_form' action='$this->actionpage' method='$this->method'>";

        //$count = 0;
		foreach($this->fieldspec AS $col_name => $colspec){
            if($colspec['show'] == 'E'){
                (empty($colspec['headname']))?$elements[$col_name]['head'] = $col_name: $elements[$col_name]['head'] = $colspec['headname'] ;
                $elements[$col_name]['head'] .= "<input type='checkbox' name='IS_CHG_$col_name' value='1'>";
                $elements[$col_name]['body'] = MakeEditField($this->fieldspec[$col_name],  $col_name, '', '', 'MS_' . $col_name, $none_entry,$this->top_dir);

                //$count ++;
			}
		}	// end foreach fieldspec
		$elements['MS_Change']['head'] = 'Change Selected';		
		$elements['MS_Change']['body'] = "<input type='hidden' name='mode'>";
		$elements['MS_Change']['body'] .= "<input type='button' name='multiedit' value='Change ' onClick=\"doMultiChange('$this->table_name')\" >";
		//$count ++;
		$elements['MS_Delete']['head'] = 'Delete Selected';
        $elements['MS_Delete']['body'] = "<input type='button' name='mutlidelete' value='Delete' onClick=\"doMultiDelete('$this->table_name')\" > ";
	    $elements['MS_Delete']['body'] .= "<input type='hidden' name='seltrans' id='seltrans'>";
		//$count ++;
        //$elements[$count]['body'] = "<input type='button' name='setmulti' value='Set' onClick='get_selections()' >";

		// Need to change the following name to something descriptive but unique and matches JS.

		if($this->use_ms_divform){
			$msrow .= $this->make_form_set($elements,"class='ms_head_class'","class='ms_row_class'");			
		}else{
			$msrow .= $this->make_element_set($elements,"class='ms_head_class'","class='ms_row_class'");
		}
        //
	
		$msrow .= "<input type='hidden' name='rqkeyvariables' value='" . $this->make_keyvariables() . "' />\n";
	
	
	
        $msrow .= "</form>\n";

	return $msrow;
	}

   	/**
	 *  make_apply_all  Method will output the Select All and Clear All buttons
	 */
	function make_apply_all(){
		return "\n<input type='button' value='Select All' onClick=\"select_by_name('$this->table_name')\"><input type='button' value='Clear All' onClick=\"unselect_by_name('$this->table_name')\">\n";
	
	}	

	/**
	 *(Also in CF_Filter and CF_AddBox)
     * @make_element set will create a line of headed elements arranged in a table
     * from an input of an element_set array.  Each row has element_name and value
     * @param array elements An array with [body] and [head] parts where head
     * elements['head'] is the title and elements['body'] is what to put in the cell
     * @param string $headset Settings for heading elements (class name, etc)
     * @param string $bodyset Settings for body elemetns (class naem, etc)
	 *
	 */
    function make_element_set($elements, $headset, $bodyset){

        if(is_array($elements)){
        foreach ($elements AS $current){
            $headtext .= "<th>" . $current['head'] . "</th>\n";
            $bodytext .=  "<td>" . $current['body'] . "</td>\n";
        }
        }
        return "<tr $headset>$headtext </tr> <tr $bodyset>$bodytext</tr>";
    }	

   	/**
	 *  make_keyvariables  Method will output a comma delimited list of variables (columns) which are keys from fieldspec
	 */
	function make_keyvariables(){
		$aftercomma = '';
		foreach($this->fieldspec as $col_name => $colspec){
			if(!empty($this->fieldspec[$col_name]['key'] )){
				$keyvariables .= $aftercomma . $col_name;
				$aftercomma = ',';
			}
		}
		return $keyvariables;
	}	
	
}


?>